/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TableReport.java
 *
 * Created on May 12, 2012, 10:26:12 PM
 */
package GUI;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import system_interface.*;
import java.util.*;
/**
 *
 * @author Jeremy
 */
public class TableReport extends Report {
    private ArrayList<Table> tables = new ArrayList<Table>();
    private SystemInterface si;
    /** Creates new form TableReport */
    public TableReport(SystemInterface si, Dimension d) {
        super(si);
        initComponents();
        this.si = si;
        this.setAutoscrolls(true);
        
        this.setPreferredSize(d);
        this.setVisible(true);
        this.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
        this.Update();
    }

    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 444, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 353, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    @Override
    public final void Update() {
        int NumTables = Integer.parseInt(si.getNumberOfTables());
        int NumDif = NumTables - this.getComponentCount();
        if(NumDif > 0) for(int i = 0; i < NumDif; i++) {
            tables.add(new Table());
            this.add(tables.get(tables.size() - 1));
        }
        else if(NumDif < 0) for(int i = 0; i < NumDif*-1; i++) {
            this.remove(tables.get(tables.size() - 1));
            tables.remove(tables.size() - 1);
        }
        for(int i = 0; i < tables.size(); i++) {
            PrintData(tables.get(i), i);
        }
        this.repaint();
    }
    
    private void PrintData(Table t, int i) {
        t.ClearText();
        t.AddText("Table " + (i + 1) + ":\n");
        t.AddText("Customers: " + si.getNumberOfCustomersAtTable(i) + '\n');
        int NumCustomers = si.getNumberOfCustomersAtTable(i);
        if(NumCustomers != 0) {
            t.AddText("Orders: " + si.getNumberOfOrdersAtTable(i) + "\n");
            t.AddText(si.getFoodStatus(i));
        }
        
    }

    
    private class Table extends javax.swing.JTextArea {
        private String txt = "";
        private final int DEFAULTSIZE = 100;
        
        public Table() {
            this.setFont(new java.awt.Font("", Font.PLAIN, 11));
            this.setText(txt);
            this.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.SoftBevelBorder.LOWERED));
            this.setAutoscrolls(false);
            this.setPreferredSize(new java.awt.Dimension(DEFAULTSIZE,DEFAULTSIZE));
            this.setVisible(true);
            this.setBackground(Color.WHITE);
        }
        
        public void ClearText() {
            txt = "";
            this.setText(txt);
        }     
        
        public void AddText(String s) {
            txt += s;
            this.setText(txt);
        }
    }
    
}
